package com.yyq.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yyq.eduservice.entity.EduSubject;
import com.yyq.eduservice.entity.excel.excelData;
import com.yyq.eduservice.entity.subjectTree.firSubjectPoint;
import com.yyq.eduservice.entity.subjectTree.secSubjectPoint;
import com.yyq.eduservice.lisenner.excelLisenner;
import com.yyq.eduservice.mapper.EduSubjectMapper;
import com.yyq.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author yyq
 * @since 2021-04-14
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void upSubjectFile(MultipartFile file,EduSubjectService subjectService) {

        try {
            InputStream in = file.getInputStream();
            EasyExcel.read(in, excelData.class,new excelLisenner(subjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public List<firSubjectPoint> getSubjectList() {
        List<firSubjectPoint> list=new ArrayList<>();

        //获取一级节点的List
        QueryWrapper<EduSubject> wrapper =new QueryWrapper<>();
        wrapper.eq("parent_id","0");   // 求eq 符合什么条件的LISt
        List<EduSubject>  oneSubjectList = baseMapper.selectList(wrapper);

        System.out.println(oneSubjectList);
        //获取二级节点的List
        QueryWrapper<EduSubject> wrapper1=new QueryWrapper<>();
        wrapper1.ne("parent_id","0");   //求Ne 不符合什么条件的List
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapper1);


        System.out.println(twoSubjectList);
        //封装到list里面
        for(int i=0;i<oneSubjectList.size();i++){
            //首先往一级节点中加入二级节点
            //获取第一个一级节点
            EduSubject eduSubject = oneSubjectList.get(i);
            firSubjectPoint one =new firSubjectPoint();
            String title=eduSubject.getTitle();
            String id=eduSubject.getId();
            one.setTitle(title);
            one.setId(id);    //往第一个节点注入了ID和title
            //接下来寻找父节点为第一个节点的

            List<secSubjectPoint>  twoList=new ArrayList<>();  //children一级节点的孩子们

            for(int j=0;j<twoSubjectList.size();j++){
                EduSubject twosbuject=twoSubjectList.get(j);
                //如果一个Edusubject对象的父id ==之前的一级节点的ID
                //生成 二级节点 并注入一级节点中
                if(twosbuject.getParentId().equals(id)){
                    System.out.println("我进来了！！！我是二级节点"+twosbuject);
                    secSubjectPoint two=new secSubjectPoint();

                    two.setId(twosbuject.getId());
                    two.setTitle(twosbuject.getTitle());
                    twoList.add(two);
                }
            }

            one.setChildren(twoList); //一级节点封装二级节点
            list.add(one);            //list封装一级节点
        }


        return list;
    }
}
